package biz

import (
	"time"
)

type DeliveryServer struct {
	stores      Store
	targetCache map[string][]PromJson
}

func NewDeliveryServer(store Store) *DeliveryServer {
	var cache = make(map[string][]PromJson)
	d := DeliveryServer{
		stores:      store,
		targetCache: cache,
	}
	go func() {
		for {
			d.flushStore()
			time.Sleep(time.Second * 10)
		}
	}()
	return &d
}

func (d *DeliveryServer) Alltargets() []PromJson {
	var (
		targets = make([]PromJson, 0)
	)
	for _, v := range d.targetCache {
		targets = append(targets, v...)
	}
	return targets
}

func (d *DeliveryServer) flushStore() {
	d.targetCache[d.stores.Name()] = d.stores.Get()
}
